package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.mapper.GradeMapper;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.entity.Grade;
import com.example.demo.entity.Message;
import com.example.demo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    private GradeMapper gradeMapper;

    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/selectAll")
    public String selectAll(){
        List<Grade> majors = gradeMapper.selectAll();
        return JSONObject.toJSONString(Message.success(majors));
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request){
        Grade majors=new Grade();
        String college = request.getParameter("college");
        String grade = request.getParameter("grade");
        if(college==null||"".equals(college)) return JSONObject.toJSONString(Message.failure("college can not null"));
        if(grade==null||"".equals(grade)) return JSONObject.toJSONString(Message.failure("grade can not null"));
        Grade gradess = gradeMapper.selectByCollegeAndGrade(college,grade);
        if(gradess!=null) return JSONObject.toJSONString(Message.failure("grade is exist,please update operation"));
        majors.setCollege(college);
        majors.setGrade(grade);
        int add = gradeMapper.add(majors);
        if(add>0) return JSONObject.toJSONString(Message.success("success add"));
        else return JSONObject.toJSONString(Message.failure("add failure"));
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request){
        int flag = Integer.valueOf(request.getParameter("flag"));
        Grade grade = gradeMapper.selectByFlag(flag);
        List<Student> stuByCollegeAndGrade = studentMapper.findStuByCollegeAndGrade(grade.getCollege(), grade.getGrade());
        if(stuByCollegeAndGrade.size()>0) return JSONObject.toJSONString(Message.failure("This is have student,can not to delete"));
        int delete = gradeMapper.delete(flag);
        if(delete>0) return JSONObject.toJSONString(Message.success("success delete"));
        else return JSONObject.toJSONString(Message.failure("delete failure"));
    }

    @RequestMapping("/update")
    public String update(HttpServletRequest request){
        int flag = Integer.valueOf(request.getParameter("flag"));
        String college=request.getParameter("college");
        String grade=request.getParameter("grade");
        List<Grade> grades = gradeMapper.selectByCollege(college);
        boolean isExistGrade = grades.stream().anyMatch(grade1 -> grade1.getGrade().equals(grade));
        if(isExistGrade) return JSONObject.toJSONString(Message.failure("update failure,because grade is exist"));
        Grade majors=new Grade();
        majors.setFlag(flag);
        if(grade!=null&&!"".equals(grade)) majors.setGrade(grade);
        int update = gradeMapper.update(majors);
        if(update>0) return JSONObject.toJSONString(Message.success("success update"));
        else return JSONObject.toJSONString(Message.failure("update failure"));
    }
}
